import common from "../dxmodules/dxCommon.js";
import log from "../dxmodules/dxLogger.js"

//Get system uptime
let time = common.getUptime();
log.info("System uptime: " + time)
//Get system remaining memory
let freemem = common.getFreemem();
log.info("Remaining system memory: " + freemem)

//Get the total system memory
let totalmem = common.getTotalmem();
log.info("Total system memory: " + totalmem)

//Get the total amount of available disk space in the system
let totaldisk = common.getTotaldisk()
log.info("Total available disk space in the system: " + totaldisk)

//Get the remaining available disk space on the system
let freedisk = common.getFreedisk()
log.info("Remaining available disk space on the system:" + freedisk)

//Get cpuId
let cpuId = common.getCpuid()
log.info("cpuId: " + cpuId)
//Get UUID
let uuid = common.getUuid()
log.info("uuid: " + uuid)
//Obtain the device's unique identifier
let sn = common.getSn()
log.info("Unique Device Identifier: " + sn)
//MAC address
let mac = common.getUuid2mac()
log.info("MAC address: " + mac)
//Get CPU usage
let freecpu = common.getFreecpu()
log.info("CPU utilization: " + freecpu)

//The sequence 123456 corresponds to the array [49, 50, 51, 52, 53, 54]. Let's MD5 it.
let ret = common.md5Hash([49, 50, 51, 52, 53, 54])
//Public Key
let pubkey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIhGA5XLhPR22MRf7ms4R3NeUyV4UvnUiu2YIrxB4RMojK8QY90760Otx6fWZsEi0gY5ysLWPZSZdu92vA4s1BsCAwEAAQ=='
//hexadecimal content
let hexData = '0c5cf58609504d91c21bf3d7275644229ca47099fb1f452336d943456caa5c305f5be1ba799b76444c7bdee7e0c33f6d9754a2281dda2a04a2a67b7ae846b0e4'
//Convert hexadecimal content into ArrayBuffer
let data = common.hexStringToArrayBuffer(hexData)
//Decryption using public key
ret = common.arrayBufferRsaDecrypt(data, pubkey)

log.info("Length of the decrypted content array: " + ret.byteLength);
//Convert arrayBuffer to hexadecimal string
log.info('Convert arrayBuffer to hexadecimal string: ', common.arrayBufferToHexString(data))
//Convert decimal to hexadecimal little-endian format
log.info('Convert the decimal number 300 to hexadecimal little-endian format: ', common.decimalToLittleEndianHex(300, 4))
//
log.info('Converting hexadecimal little-endian format 2c01 to decimal: ', common.littleEndianToDecimal('2c01'))

//Byte array to hexadecimal
log.info('Byte array to hexadecimal: ', common.arrToHex([10]))
//Hexadecimal to byte array
log.info('Hexadecimal to byte array conversion: ', common.hexToArr(common.stringToHex('123456')))
//
log.info('Calculate the MD5 hash value: ', common.arrToHex(common.md5Hash([49, 50, 51, 52, 53, 54])))



//It needs to be converted to ArrayBuffer before being passed in.
ret = common.hmacMd5Hash(str2ab('abcdefg'), str2ab("1234567887654321"))
log.info("hmacMd5Hash result: ", arrayBufferToHexString(ret));
//You can directly write strings; the effect is the same as hmacMd5Hash.
ret = common.hmac('abcdefg', "1234567887654321")
log.info("HMAC results: ", arrayBufferToHexString(ret));








//--------------------------------------private methods----------------------------------
// String to ArrayBuffer
function str2ab(str) {
  var buf = new ArrayBuffer(str.length); // 2 bytes for each char
  var bufView = new Uint8Array(buf);
  for (var i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

function arrayBufferToHexString(buffer) {
  const view = new Uint8Array(buffer);
  let hexString = '';

  for (let i = 0; i < view.length; i++) {
    const byte = view[i].toString(16).padStart(2, '0');
    hexString += byte;
  }

  return hexString
}
